﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using Bling.DSL;
using Bling.Util;
using Bling.Core;
using Bling.Shapes;
using Bling.WPF;

namespace BookFlipDemo {
  class FreeFlipPage : Canvas {
    private bool isDown = false;

    private ThumbBl[] anchorThumbs = new ThumbBl[5];

    private FlipPage currPage;
    
    public FreeFlipPage( FrameworkElement elem1, FrameworkElement elem2, bool samePage) {

      var page = new FlipPage(elem1, elem2, samePage);

      currPage = page;

      page.EnableAssistLine = false;
      page.Bl().LeftTop = new Point(0, 0);
      page.Bl().Size = this.Bl().Size;

      this.Children.Add(page);

      page.MouseLeftButtonDown += page_MouseDown;

      page.MouseMove += page_MouseMove;

      page.MouseLeftButtonUp += page_MouseUp;

      var random = new Random();
      for (int i = 0; i < anchorThumbs.Length; i++) {
        anchorThumbs[i] = new ThumbBl(this) { Background = Brushes.Red, CanDrag = true };
        anchorThumbs[i].CenterPosition = new Double2Bl(this.currPage.Bl().Width * random.NextDouble(), this.currPage.Bl().Height * random.NextDouble());

      }
    }

    private void page_MouseDown(Object x, MouseEventArgs y) {
      var flipPoint = y.GetPosition((UIElement)x);


      Mouse.Capture((UIElement)x);

      isDown = true;

      currPage.AnchorPoints.Clear();
      //currPage.AnchorPoints.Add(currPage.FrontPage.Bl().RightTop.Value);
      //currPage.AnchorPoints.Add(currPage.FrontPage.Bl().RightBottom.Value);
      var pageRect = new Rect(new Point(0, 0), (Point) currPage.Bl().Size);
      foreach (var thumb in anchorThumbs) {
        if ( pageRect.Contains ((Point) (thumb.CenterPosition)) )
          currPage.AnchorPoints.Add((Point) (thumb.CenterPosition));
      }            

      if (pageRect.Contains(flipPoint)) {
        Point realFlipPoint;

        if (currPage.StartFlip(
          out realFlipPoint,
          currPage.FrontPage.Bl().CenterSize,
          flipPoint)) {

          currPage.Flip(flipPoint);
#if false
          var rand = new Random();
          var startTime = System.Environment.TickCount / 1000.0;

          for (int i = 0; i < 200; i++) {
            currPage.Flip(new PointBl(rand.NextDouble() * currPage.Bl().Width.Value, rand.NextDouble() * currPage.Bl().Height.Value));
          }
          var endTime = System.Environment.TickCount / 1000.0;
          MessageBox.Show((endTime - startTime).ToString());

#endif
          return;
        }
      }

      isDown = false;
      Mouse.Capture(null);

    }

    private void page_MouseMove(Object x, MouseEventArgs y) {
      if (isDown) {

        var flipPoint = y.GetPosition((UIElement)x);
        currPage.Flip(flipPoint);
      }
    }

    private void page_MouseUp(Object x, MouseEventArgs y) {
      if (isDown) {
        isDown = false;
        Mouse.Capture(null);

        currPage.StopFlip();



      }


    }

  }

  class FreeFlipCanvas : Canvas {

    public static readonly ImageSourceBl Autumn = (new BitmapImage("Resources/Autumn.jpg".MakePackUri(typeof(FreeFlipCanvas))));
    public static readonly ImageSourceBl Desert = (new BitmapImage("Resources/Desert.jpg".MakePackUri(typeof(FreeFlipCanvas))));
    public static readonly ImageSourceBl Dock = (new BitmapImage("Resources/Dock.jpg".MakePackUri(typeof(FreeFlipCanvas))));
    public FreeFlipCanvas() {

      var page1 = new FreeFlipPage(
        new ImageBl() { Stretch = Stretch.Fill, Source = (Autumn) },
        new ImageBl() { Stretch = Stretch.Fill, Source = (Desert) },                
        false );

      page1.Bl().Size = new Double2Bl(200, 250);
      page1.Bl().ZIndex = 2;
      var moveThumb1 = new ThumbBl(this) { Background = Brushes.Blue, CanDrag = true };
      page1.Bl().CenterPosition = moveThumb1.CenterPosition;
      moveThumb1.LeftTop = new Double2Bl(400, 200);
      moveThumb1.ZIndex = 10;

      //page1.Bl().RenderTransform.Rotate = (30.0).Bl().ToDegrees;
      this.Children.Add(page1);

      var page2 = new FreeFlipPage(
        new ImageBl() { Stretch = Stretch.Fill, Source = (Dock) },
        new ImageBl() { Stretch = Stretch.Fill, Source = (Dock) },
        true);

      page2.Bl().Size = new Double2Bl(250, 200);
      page2.Bl().ZIndex = 2;
      var moveThumb2 = new ThumbBl(this) { Background = Brushes.Blue, CanDrag = true };
      page2.Bl().CenterPosition = moveThumb2.CenterPosition;
      moveThumb2.LeftTop = new Double2Bl(700, 400);
      moveThumb2.ZIndex = 10;
      page2.Bl().RenderTransform.Rotate = (30.0).Bl().ToDegrees();

      this.Children.Add(page2);

    }

  }
}